<template>
  <el-drawer
    model-value="dialogVisible"
    :title="dialogTitle"
    width="30%"
    @close="handleClose"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="角色名" prop="name">
        <el-input v-model="form.name" maxlength="30" />
      </el-form-item>

      <el-form-item label="角色编码" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" :rows="4" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleConfirm(formRef)"
          >确认</el-button
        >
        <el-button @click="handleClose">取消</el-button>
      </span>
    </template>
  </el-drawer>
</template>
<script lang="ts">
export default {
  name: "roleDialog",
};
</script>
<script setup lang="ts">
import { defineEmits, defineProps, ref, watch } from "vue";
import { ElMessage } from "element-plus";
import {
  checkRoleName,
  checkRoleCode,
  getRoleById,
  saveRole,
} from "@/api/role";
import type { FormInstance, FormRules } from "element-plus";

const tableData = ref([]);

const props = defineProps({
  roleId: {
    type: Number,
    default: -1,
    required: true,
  },
  dialogTitle: {
    type: String,
    default: "",
    required: true,
  },
  dialogVisible: {
    type: Boolean,
    default: false,
    required: true,
  },
});
interface RuleForm {
  id: number;
  name: string;
  code: string;
  remark: string;
}

const form = ref<RuleForm>({
  id: -1,
  name: "",
  code: "0",
  remark: "",
});

const checkRoleNameHandler = async (rule, value, callback) => {
  if (form.value.id == -1) {
    const res = await checkRoleName({
      name: form.value.name,
    });
    if (res.data.code == 500) {
      callback(new Error("角色名已存在！"));
    } else {
      callback();
    }
  } else {
    callback();
  }
};
const checkRoleCodeHandler = async (rule, value, callback) => {
  if (form.value.id == -1) {
    const res = await checkRoleCode({
      code: form.value.code,
    });
    if (res.data.code == 500) {
      callback(new Error("角色编码已存在！"));
    } else {
      callback();
    }
  } else {
    callback();
  }
};
const rules = ref({
  name: [
    { required: true, message: "请输入角色名" },
    { required: true, validator: checkRoleNameHandler, trigger: "blur" },
  ],
  code: [
    { required: true, message: "请输入角色编码" },
    { required: true, validator: checkRoleCodeHandler, trigger: "blur" },
  ],
});

const formRef = ref<FormInstance>();

const initFormData = async (id) => {
  const res = await getRoleById(id);
  form.value = res.data.data.sysRole;
};

watch(
  () => props.dialogVisible,
  () => {
    let id = props.roleId;
    if (id > 0) {
      initFormData(id);
    } else {
      form.value = {
        id: -1,
        name: "",
        code: "",
        remark: "",
      };
      formRef && formRef.value && formRef.value.resetFields();
    }
  }
);

/*创建一个事件名为update:modelValue的initUserList事件*/
const emits = defineEmits(["update:modelValue", "initRoleList"]);

const handleClose = () => {
  emits("update:modelValue", false);
};

const handleConfirm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (valid) {
      let result = await saveRole(form.value);
      let data = result.data;
      if (data.code == 200) {
        ElMessage.success(data.msg);
        formEl.resetFields();
        emits("initRoleList");
        handleClose();
      }
    } else {
      console.log("fail");
    }
  });
};
</script>

<style lang="less" scoped>
</style>